﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using SweetSurvey.Models;

namespace SweetSurvey.Controllers
{
    public class AccountController : ControllerBase
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        [HttpPost]
        public ActionResult LogOnByAjax(FormCollection collection)
        {
            var email = collection["email"];
            var password = collection["password"];
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { return Content(string.Empty); }

            if (MembershipService.ValidateUser(email, password))
            {
                FormsService.SignIn(email, true);
                return Content("success|" + email);
            }

            return Content(string.Empty);
        }

        public ActionResult LogOffByAjax()
        {
            FormsService.SignOut();
            return Content("success");
        }

        [HttpPost]
        public ActionResult RegisterByAjax(FormCollection collection)
        {
            var email = collection["email"];
            var password = collection["password"];

            MembershipCreateStatus createStatus = MembershipService.CreateUser(email, password, email);

            if (createStatus == MembershipCreateStatus.Success)
            {
                FormsService.SignIn(email, true);
                return Content("success|" + email);
            }
            return Content(createStatus.ToString());
        }

        [HttpPost, Authorize]
        public ActionResult ChangePasswordByAjax(FormCollection collection)
        {
            string currentPassword = collection["currentPassword"];
            if (string.IsNullOrEmpty(currentPassword)) { return Content("Please input the current password."); }

            string newPassword = collection["newPassword"];
            if (string.IsNullOrEmpty(currentPassword)) { return Content("Please input the current password."); }

            string confirmNewPassword = collection["confirmNewPassword"];
            if (string.IsNullOrEmpty(confirmNewPassword)) { return Content("Please input again your new password to confirm."); }
            if (!newPassword.Equals(confirmNewPassword)) { return Content("The new passwords you entered in the two times are different."); }

            if (MembershipService.ChangePassword(User.Identity.Name, currentPassword, newPassword))
            {
                return Content("success");
            }
            else
            {
                return Content("The current password is incorrect or the new password is invalid.");
            }
        }

        [HttpPost]
        public ActionResult AdministratorExists()
        {
            return Json(MembershipService.DefaultAdministratorExists());
        }

        [HttpPost]
        public ActionResult CreateAdministrator()
        {
            MembershipService.CreateDefaultAdministrator();
            if (MembershipService.ValidateUser(
                    MembershipService.AdministratorUserName,
                    MembershipService.AdministratorPassword))
            {
                FormsService.SignIn(MembershipService.AdministratorUserName, true);
                return Json("Success");
            }
            else
            {
                throw new Exception("Failed to create default administrator.");
            }
        }

    }
}
